package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class HelloJob {


    @XxlJob("demoJobHandler2")
    public void sampleJob(){
        //总分片数
        int shardTotal = XxlJobHelper.getShardTotal();
        //当前实行的分片
        int shardIndex = XxlJobHelper.getShardIndex();

        //业务数据
        List<Integer> data = getData();
        //让业务数据 % 总分片数
        for (Integer integer : data) {
            if(integer % shardTotal == shardIndex){
                System.out.println("当前分片："+shardIndex+"，执行了任务项为："+integer);
            }
        }

    }

    /**
     * 模拟任务数据（跟业务有关系）
     * @return
     */
    private List<Integer> getData(){

        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }

        return list;

    }
}
